package com.example.component;

import com.example.enums.ProductOrderPayTypeEnum;
import com.example.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/6/2015:47
 */
@Component
public class PayFactory {

    @Autowired
    private AliPayStrategy aliPayStrategy;

    @Autowired
    private WechatPayStrategy wechatPayStrategy;

    /**
     * 创建支付，简单工厂模式
     * @param payInfoVO
     * @return
     */
    public String pay(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(ProductOrderPayTypeEnum.ALI_PAY.name().equalsIgnoreCase(payType)){
            //支付宝支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeUnifiedOrder(payInfoVO);
        }else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equalsIgnoreCase(payType)){
            //微信支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeUnifiedOrder(payInfoVO);
        }
        return "";
    }

    /**
     * 关闭订单方法
     * @param payInfoVO
     * @return
     */
    public String closeOrder(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(ProductOrderPayTypeEnum.ALI_PAY.name().equalsIgnoreCase(payType)){
            //支付宝支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeCloseOrder(payInfoVO);
        }else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equalsIgnoreCase(payType)){
            //微信支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeCloseOrder(payInfoVO);
        }
        return "";
    }

    /**
     * 查询订单支付状态方法
     * @param payInfoVO
     * @return
     */
    public String queryPayStatus(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(ProductOrderPayTypeEnum.ALI_PAY.name().equalsIgnoreCase(payType)){
            //支付宝支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeQueryPayStatus(payInfoVO);
        }else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equalsIgnoreCase(payType)){
            //微信支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeQueryPayStatus(payInfoVO);
        }
        return "";
    }

    /**
     * 退款方法
     * @param payInfoVO
     * @return
     */
    public String refund(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(ProductOrderPayTypeEnum.ALI_PAY.name().equalsIgnoreCase(payType)){
            //支付宝支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeRefund(payInfoVO);
        }else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equalsIgnoreCase(payType)){
            //微信支付策略
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeRefund(payInfoVO);
        }
        return "";
    }


}
